// pages/shop_add/shop_add.js
const api = require('../../utils/util.js');
const service = require('../../utils/service.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    commentList: [],
    tabIndex: 0,
    score: 0, //0为全部，1为差评，2为中评，3为好评
    reply_images: '',
    page: 1,
    show_reply: [0, 1, 0, 0, 0],
    seller_name: wx.getStorageSync('shop_data').nickname,
    buyer_name: '',
    comment_id: '',
    all_num: 0,
    good_num: 0,
    middle_num: 0,
    bad_num: 0,
    replyBol: false,
    replyFocus: false,
    replyName: '回复-天叶蓝',
    replyVal: '',
    scrollTop: 0,
    replySubmit: false,
    replyIndex: 0
  },
  init: function() {
    this.getCommentList();
  },
  getCommentList: function(commentListNull) {
    let self = this,
      _null = typeof commentListNull == 'boolean' ? commentListNull : false;
    let shop_code = wx.getStorageSync('shopInfo').shop_code;
    let shopData = wx.getStorageSync('shop_data_shop');
    api.ajax(service.url('getCommentList'), {
      unionid: shopData.shop_unionid,
      token: shopData.token,
      token_key: shopData.token_key,
      shop_code: shop_code,
      state: self.data.score, //0为全部，1为差评，2为中评，3为好评
      page: self.data.page,
    }, function(res) {
      // var shopCode = res.data.shop_code; // 商店id
      console.log("评价列表" + self.data.score + "：" + JSON.stringify(res));
      if (res.code == 0) {
        if (_null) {
          self.setData({
            commentList: []
          });
        }
        if (res.data.comment_list.length == 0) {
          api.toast('没有更多数据...');
        } else {
          for (let i = 0; i < res.data.comment_list.length; i++) {
            res.data.comment_list[i]['star_arr'] = [];
            for (let j = 0; j < res.data.comment_list[i].star_num; j++) {
              res.data.comment_list[i]['star_arr'].push('1');
            }

            if (res.data.comment_list[i].reply_content) {
              self.setData({
                [`show_reply[${i}]`]: 1
              });
            } else {
              self.setData({
                [`show_reply[${i}]`]: 0
              });
            }
          }
          self.setData({
            commentList: self.data.commentList.concat(res.data.comment_list),
            all_num: res.data.all_num,
            good_num: res.data.good_num,
            middle_num: res.data.middle_num,
            bad_num: res.data.bad_num,
            page: self.data.page + 1
          });
        }
      } else {
        api.toast(res.message || '数据出现错误');
      }
    });
  },
  //--商家回复评论
  writeComment: function(et) {
    let self = this;
    this.setData({
      replyBol: true,
      replyFocus: true,
      comment_id: et.currentTarget.dataset.id,
      buyer_name: et.currentTarget.dataset.nickname,
      replyName: '回复' + et.currentTarget.dataset.nickname,
      replyIndex: +et.currentTarget.dataset.index
    });
  },
  //--关闭商家回复评论
  closeWriteComment: function() {
    this.setData({
      replyBol: false,
      replyFocus: false,
    });
  },
  //--监听inputfocus
  focusEvent: function() {
    this.setData({
      replyVal: '',
      replySubmit: false
    });
  },
  //--监听input输入
  valid: function(et) {
    let self = this;
    let val = et.detail.value.replace(/\s+/g, "");
    let _submit = val != '' ? true : false;
    this.setData({
      replyVal: val,
      replySubmit: _submit
    });
  },
  //--商家回复评论接口调用
  sellerOrderComment: function() {
    let self = this;
    let shop_code = wx.getStorageSync('shopInfo').shop_code;
    let shopData = wx.getStorageSync('shop_data');
    if (this.data.replyVal == '') {
      api.toast("请输入评论内容！");
      return;
    }
    this.setData({
      replyBol: false,
      replyFocus: false,
    });
    api.ajax(service.url('sellerOrderComment'), {
      unionid: shopData.shop_unionid,
      token: shopData.token,
      token_key: shopData.token_key,
      shop_code: shop_code,
      comment_id: self.data.comment_id,
      reply_content: self.data.replyVal,
      reply_images: '',
    }, function(res) {
      console.log("商家回复评论：" + JSON.stringify(res))
      if (res.code == 0) {
        self.setData({
          [`commentList[${self.data.replyIndex}].reply_content`]: self.data.replyVal,
          [`show_reply[${self.data.replyIndex}]`]: 1
        });
      } else {
        api.toast(res.message || '数据出现错误！');
      }
    });
  },
  tab: function(et) {
    let index = +et.currentTarget.dataset.index;
    let _score = index == 0 || index == 2 ? index : index == 1 ? 3 : 1;
    this.setData({
      tabIndex: index,
      score: _score,
      page: 1
    });
    this.getCommentList(true);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.init();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.getCommentList();
  },
  /**
   * 监听用户滑动页面事件
   */
  onPageScroll: function(et) {
    this.setData({
      scrollTop: et.scrollTop
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})